package com.heima.user.controller.v1;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.user.service.ApUserRealnameService;
import com.heima.user.service.ApUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/api/v1/auth")
public class ApUserAuthController {
    @Autowired
    private ApUserService apUserService;
    @Autowired
    private ApUserRealnameService apUserRealnameService;

    @PostMapping("/list")
    public ResponseResult list(@RequestBody AuthDto dto) {
        List<AuthDto> authDtoList = new ArrayList<>();
        Page<ApUser> page = new Page<>(dto.getPage(), dto.getSize());
         page = apUserService.page(page);
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());


        for (ApUser apUser : page.getRecords()) {
            AuthDto authDto = new AuthDto();
            Integer id = apUser.getId();
            LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<ApUserRealname>()
                    .eq(dto.getStatus() != null, ApUserRealname::getStatus,
                            dto.getStatus()).eq(id != null, ApUserRealname::getUserId, id);
            ApUserRealname apUserRealname = apUserRealnameService.getOne(queryWrapper);
            if (apUserRealname != null) {
                authDto.setIdno(apUserRealname.getIdno());
                authDto.setStatus(Integer.valueOf(apUserRealname.getStatus()));
                authDto.setName(apUserRealname.getName());
                authDto.setIdentityAuthentication(apUser.getIdentityAuthentication());
                authDtoList.add(authDto);
            }
        }
        pageResponseResult.setData(authDtoList);
        return  pageResponseResult;

    }
}
